import { create } from 'zustand'

interface State {
  showSuggestion: boolean;
  activeSuggestionId: string | null;
  setShowSuggestion: (b: boolean) => void;
  setActiveSuggestionId: (
    n: string | null | ((n: string | null) => string | null)
  ) => void;
}

export const useSuggestionStore = create<State>((set) => ({
  showSuggestion: true,
  activeSuggestionId: null,
  setShowSuggestion: (showSuggestion: boolean) => {
    set({ showSuggestion })
  },
  setActiveSuggestionId: (
    activeSuggestionId: string | null | ((n: string | null) => string | null),
  ) => {
    if (typeof activeSuggestionId === 'function') {
      set(state => ({
        activeSuggestionId: activeSuggestionId(state.activeSuggestionId),
      }))
    } else {
      set({ activeSuggestionId })
    }
  },
}))
